Skip to main content
ICT
Lesson A17 - Quadratic Sorting Algorithms
 
Main Previous Next
Title Page >  
Summary >  
Lesson A1 >  
Lesson A2 >  
Lesson A3 >  
Lesson A4 >  
Lesson A5 >  
Lesson A6 >  
Lesson A7 >  
Lesson A8 >  
Lesson A9 >  
Lesson A10 >  
Lesson A11 >  
Lesson A12 >  
Lesson A13 >  
Lesson A14 >  
Lesson A15 >  
Lesson A16 >  
Lesson A17 >  
Lesson A18 >  
Lesson A19 >  
Lesson A20 >  
Lesson A21 >  
Lesson A22 >  
Lesson AB23 >  
Lesson AB24 >  
Lesson AB25 >  
Lesson AB26 >  
Lesson AB27 >  
Lesson AB28 >  
Lesson AB29 >  
Lesson AB30 >  
Lesson AB31 >  
Lesson AB32 >  
Lesson AB33 >  
Vocabulary >  
 

D. Insertion Sort page 6 of 11

  1. Insertion Sort takes advantage of the following fact.

    If A < B and B < C, then it follows that A < C. We can skip the comparison of A and C.

  1. Consider the following partially sorted list of numbers.

    2 5 8 3 9 7

    The first three values of the list are sorted. The 4th value in the list, (3), needs to move back in the list between the 2 and 5.

    This involves two tasks, finding the correct insert point and a right shift of any values between the start and insertion point.

  2. The code follows.

    void insertionSort(ArrayList <Integer> list){
      for (int outer = 1; outer < list.size(); outer++){
        int position = outer;
        int key = list.get(position);

        // Shift larger values to the right
        while (position > 0 && list.get(position - 1) > key){
          list.set(position, list.get(position - 1));
          position--;
        }
        list.set(position, key);
      }
    }

  3. By default, a list of one number is already sorted. Hence the outer loop skips position 0 and ranges from positions 1 to list.size(). For the sake of discussion, let us assume a list of 6 numbers.

  4. For each pass of outer, the algorithm will determine two things concerning the value stored in list[outer]. First, it finds the location where list[outer] needs to be inserted in the list. Second, it does a right shift on sections of the array to make room for the inserted value if necessary.

  5. Constructing the inner while loop is an appropriate place to apply DeMorgan’s laws:

    1. The inner while loop postcondition has two possibilities:
      The value (key) is larger than its left neighbor.
      The value (key) moves all the way back to position 0.

    2. This can be summarized as:

      (0 == position || list.get(position - 1) <= key)

    3. If we negate the loop postcondition, we get the while loop boundary condition:

      (0 != position && list.get(position - 1) > key)

    4. This can also be rewritten as:

      ((position > 0) && (list.get(position - 1) > key))

  6. The two halves of the boundary condition cover these situations:

    (position > 0) -> we are still within the list, keep processing

    list[position - 1] > key -> the value in list[pos-1] is larger than key, keep moving left (position--) to find the first value smaller than key.

  7. The Insertion Sort algorithm is appropriate when a list of data is kept in sorted order with infrequent changes. If a new piece of data is added, probably at the end of the list, it will get quickly inserted into the correct position in the list. Many of the other values in the list do not move and the inner while loop will not be used except when inserting a new value into the list.

  8. Here is the same list of six integers to practice Insertion Sort. (Answers are found in Lesson A17 Handout, Sorting Answers.)

outer
57
95
88
14
25
6
1
_____
_____
_____
_____
_____
_____
2
_____
_____
_____
_____
_____
_____
3
_____
_____
_____
_____
_____
_____
4
_____
_____
_____
_____
_____
_____
5
_____
_____
_____
_____
_____
_____

 

Main Previous Next
Contact
 © ICT 2006, All Rights Reserved.